﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Think.Lenovo.Com.Models;
using Think.Lenovo.Com.DataAccess;
namespace Think.Lenovo.Com.Controllers
{
    public class PowerController:Controller
    {
        public ActionResult ViewManagePower()
        {
            #region //step 1. 得到所有权限
            Power p = new Power();
            LinkedList<PowerModel> list = p.GetAllPower();
            ViewData["list"] = list;
            #endregion
            return View("/Views/ViewManagePower.cshtml");
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="model">权限实体对象</param>
        /// <returns>错误信息以及是否插入成功标志</returns>
        public ActionResult AddPower(PowerModel model)
        {
            LinkedList<String> errors = new LinkedList<string>();
            Dictionary<Object, Object> result = new Dictionary<object, object>();
            String isSuccess = "1";
            Power p = new Power();
            if (String.IsNullOrEmpty(model.Code))
            {
                errors.AddLast("权限编码不能为空!");
            }
            if (String.IsNullOrEmpty(model.Name))
            {
                errors.AddLast("权限编码不能为空!");
            }
            if (String.IsNullOrEmpty(model.Description))
            {
                errors.AddLast("权限描述不能为空!");
            }
            if (errors.Count < 1)
            {
                long id = p.AddPower(model);
                if (id < 1)
                {
                    errors.AddLast("添加权限时,发生错误");
                    isSuccess = "-1";
                }
            }
            result.Add("errors",errors);
            result.Add("isSuccess", isSuccess);
            JsonResult jr = new JsonResult();
            jr.Data = result;
            return jr;
        }
    }
}
